package cn.edu.ecut.se2024.controller;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ecut.se2024.exception.OperationFailedException;
import cn.edu.ecut.se2024.exception.UnauthorizedException;
import cn.edu.ecut.se2024.model.UserMenu;
import cn.edu.ecut.se2024.service.SysService;

@Controller
public class SysController extends AuthorizedController {	
	@Autowired
	private SysService sysService;

	@RequestMapping("/sys.toChangePassword")
    public String changePassword(HttpServletRequest request) {
        return SYS_MAIN_PAGE; 
    }

	@PostMapping("/sys.changePassword")
    public String changePassword(HttpServletRequest request, 
    		@RequestParam @Validated String oldPswd, 
    		@RequestParam @Validated String newPswd) throws OperationFailedException {
		UserMenu userMenu = (UserMenu)request.getSession().getAttribute(SYS_USER_MENU);
		sysService.changePassword(userMenu.getUser().getUserId(), oldPswd, newPswd);
		return SYS_MAIN_PAGE; 
    }


}
